<template>
  <div class="app-container">
    <el-form :inline="true" label-width="80px">
      <el-form-item label="省">
        <el-select :filterable="true" @keyup.enter.native="handleQuery" v-model="queryParams.provinceId" placeholder="省"
                   clearable size="small" @clear="provinceClear" @change="provinceChange">
          <el-option
            v-for="dict in areaList"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="市">
        <el-select filterable @keyup.enter.native="handleQuery" v-model="queryParams.cityId" placeholder="市" clearable
                   size="small">
          <el-option
            v-for="dict in cityList"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="推荐类型">
        <el-select
          style="width: 100px;"
          @keyup.enter.native="handleQuery" v-model="queryParams.app" placeholder="推荐类型" clearable
          size="small">
          <el-option
            v-for="dict in appOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="状态">
        <el-select
          style="width: 100px;"
          @keyup.enter.native="handleQuery" v-model="queryParams.status" placeholder="状态" clearable
          size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleInsert"
                   v-hasPermi="['main:recommend:add']">添加
        </el-button>
      </el-form-item>

    </el-form>
    <el-table v-loading="loading" :data="postList" @sort-change="sortChange">
      <el-table-column label="推荐类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.app===3">门店推荐</span>
          <span v-else>美发师推荐</span>
        </template>
      </el-table-column>
      <el-table-column label="关联城市" align="center" prop="area.name"/>
      <el-table-column label="关联信息" align="center">
        <template slot-scope="scope">
          <el-link type="primary" v-if="scope.row.app===3" @click.native="findStore(scope.row)">
            {{scope.row.store.storename}}-{{scope.row.store.mobile}}
          </el-link>
          <el-link type="primary" @click.native="findStylist(scope.row)" v-else>
            {{scope.row.stylistUser.nickname}}-{{scope.row.stylistUser.mobile}}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span class="text-danger" v-if="scope.row.status===0">已下线</span>
          <span class="text-info" v-if="scope.row.status===1">待上线</span>
          <span class="text-navy" v-if="scope.row.status===2">已上线</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" align="center" sortable="custom"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleOnline(scope.row)"
            v-hasPermi="['main:recommend:edit']"
            v-show="scope.row.status===1"
          >上线
          </el-button>
          <el-button
            size="mini"
            type="warning"
            @click="handleOffline(scope.row)"
            v-hasPermi="['main:recommend:edit']"
            v-show="scope.row.status===2"
          >下线
          </el-button>
          <el-button
            size="mini"
            type="success"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['main:recommend:edit']"
            v-show="scope.row.status!==0"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['main:recommend:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="600px" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="推荐类型" prop="app">
          <el-radio-group v-model="form.app" v-for="item in appOptions" @change="change">
            <el-radio :label="item.dictValue" border style="margin-right: 10px;">{{item.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="关联城市" prop="cityId">
          <el-select style="width: 100px" :filterable="true" @keyup.enter.native="handleQuery" v-model="form.provinceId"
                     placeholder="省"
                     clearable size="small" @clear="provinceClear" @change="provinceChange">
            <el-option
              v-for="dict in areaList"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          -
          <el-select style="width: 100px" filterable @keyup.enter.native="handleQuery" v-model="form.cityId"
                     placeholder="市" clearable
                     size="small">
            <el-option
              v-for="dict in cityList"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="关联用户" prop="joinId">
          <el-select v-model="form.joinId"
                     filterable
                     remote
                     reserve-keyword
                     style="width: 300px;"
                     placeholder="请输入手机号码查询"
                     :remote-method="remoteMethod"
                     :loading="selectLoading"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.nickname+'-'+item.mobile"
              :value="item.id">
              <span style="float: left">{{ item.nickname }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{item.mobile}}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="显示排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="addLoading" @click.native.prevent="submitForm">
          <span v-if="!addLoading">提 交</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {listRecommend, editRecommend, removeRecommend, addRecommend} from "@/api/main/app/recommend";
  import {area} from "@/utils/area";
  import {getStylistByMobile, getStoreByMobile, getStylistById, getStoreById} from "@/api/common";

  export default {
    data() {
      return {
        total: 0,
        selectLoading: false,
        addLoading: false,
        title: '',
        open: false,
        postList: [],
        options: [],
        loading: false,
        areaList: undefined,
        cityList: undefined,
        appOptions: [{"dictLabel": "美发师推荐", "dictValue": "2"}, {"dictLabel": "门店推荐", "dictValue": "3"}],
        statusOptions: [{"dictLabel": "已下线", "dictValue": "0"}, {
          "dictLabel": "待上线",
          "dictValue": "1"
        }, {"dictLabel": "已上线", "dictValue": "2"}],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          provinceId: undefined,
          cityId: undefined,
          app: undefined
        },
        form: {
          provinceId: undefined,
          cityId: undefined,
          sort: 0,
          app: '2',
          joinId: undefined
        },
        rules: {
          cityId: [
            {required: true, message: '请选择城市', trigger: 'blur'}
          ],
          joinId: [
            {required: true, message: '请选择关联用户', trigger: 'blur'}
          ],
        }
      }
    },
    created() {
      area().then(response => {
        this.areaList = response;
      });

      this.getList();
    },
    methods: {
      getList() {
        this.loading = true;
        listRecommend(this.queryParams).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
          console.log(this.postList);
        });
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      handleInsert() {
        this.title = '新增';
        this.open = true;
      },
      handleOffline(row) {
        let _this = this;
        this.$confirm(`确定要更改当前行的的状态为下线?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          editRecommend({id: row.id, status: 0}).then(response => {
            _this.msgSuccess("下线成功");
            _this.getList();
          });
        }).catch(function (e) {
          console.log(e);
        });
      },
      handleOnline(row) {
        let _this = this;
        this.$confirm(`确定要更改当前行的的状态为上线?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "info"
        }).then(function () {
          editRecommend({id: row.id, status: 2}).then(response => {
            _this.msgSuccess("上线成功");
            _this.handleQuery();
          });
        }).catch(function (e) {
          console.log(e);
        });
      },
      handleUpdate(row) {
        console.log(row);
        this.form = {
          id: row.id,
          provinceId: row.provinceId,
          cityId: row.cityId,
          sort: row.sort,
          app: row.app + '',
          joinId: row.joinId
        };
        this.provinceChange(row.provinceId);
        if (row.app === 2) {
          this.options.push({id: row.stylist.id, nickname: row.stylistUser.nickname, mobile: row.stylistUser.mobile});
        } else {
          this.options.push({id: row.store.id, nickname: row.store.storename, mobile: row.store.mobile});
        }
        this.title = '编辑';
        this.open = true;
      },
      handleDelete(row) {
        let _this = this;
        this.$confirm(`确定要删除当前行的记录?删除将找不回此记录.`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "error"
        }).then(function () {
          removeRecommend(row.id).then(response => {
            _this.msgSuccess("删除成功");
            _this.getList();
          });
        }).catch(function (e) {
          console.log(e);
        });
      },
      provinceClear() {
        this.queryParams.cityId = '';
        this.cityList = [];
      },
      provinceChange(value) {
        if (value) {
          console.log(value);
          this.queryParams.cityId = '';
          this.cityList = this.areaList.filter(item => item.value === value)[0].children;
          console.log(this.cityList);
        }
      },
      sortChange(params) {
        if (params.prop === 'sort') {
          let sort = 'T1.sort';
          if (params.order === 'ascending') {
            this.queryParams.orderByColumn = sort + ' ASC';
          } else {
            this.queryParams.orderByColumn = sort + ' DESC';
          }
        }
        this.getList();
      },
      submitForm() {
        let _this = this;
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.addLoading = true;
            if (this.form.id) {
              editRecommend(this.form).then(response => {
                _this.msgSuccess("编辑成功");
                _this.addLoading = false;
                _this.cancel();
                _this.getList();
              }).catch(function (e) {
                _this.addLoading = false;
              });
            } else {
              addRecommend(this.form).then(response => {
                _this.addLoading = false;
                _this.msgSuccess("新增成功");
                _this.cancel();
                _this.getList();
              }).catch(function (e) {
                _this.addLoading = false;
              });
            }

          }
        });
      },
      reset() {
        this.form = {
          provinceId: undefined,
          cityId: undefined,
          sort: 0,
          app: '2',
          joinId: undefined
        };
        this.options = [];
        this.resetForm("form");
      },
      cancel() {
        this.open = false;
        this.reset();
      },
      change(val) {
        this.options = [];
        this.form.joinId = undefined;
      },
      remoteMethod(query) {
        if (query && query.trim()) {
          this.selectLoading = true;
          if (this.form.app === '2') {
            getStylistByMobile({mobile: query}).then(response => {
              console.log(response);
              this.options = response.data;
              this.selectLoading = false;
            });
          } else {
            getStoreByMobile({mobile: query}).then(response => {
              console.log(response);
              this.options = response.data;
              this.selectLoading = false;
            });
          }

        }
      },
      findStore(row) {
        console.log(row);
        getStoreById(row.store.id).then(res => {
          //this.open=true;
          console.log(res);
        });
      },
      findStylist(row) {
        getStylistById(row.stylist.id).then(res => {
          console.log(res);
        });
      }
    }
  }
</script>

<style>

</style>
